package com.car.serve.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import com.car.serve.Param.PageServeShouHouParam;
import com.car.serve.Param.PageStoresShouHouParam;
import com.car.serve.Param.addServeShouHouParam;
import com.car.serve.entity.ServeShouhou;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageServeShouHouForm;
import com.car.serve.form.PageStoresShouHouForm;
import com.car.serve.form.addServeShouHouForm;
import com.car.serve.service.ServeShouhouService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/serveShouhou")
@Api(tags = "服务售后信息")
public class ServeShouhouController {
    @Resource
    private ServeShouhouService serveShouhouService;
    @PostMapping("/addServeShouHou")
    @ApiOperation(value = "新增售后服务")
    public Result addServeShouHou(@RequestBody addServeShouHouForm form)throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        addServeShouHouParam param = BeanUtil.toBean(form, addServeShouHouParam.class);
        param.setServeUserid(userId);
        serveShouhouService.addServeShouHou(param);
        return Result.getResult().setMsg("新增服务售后成功");
    }
    @PostMapping("/findBuyIdShouHouPage.pt")
    @ApiOperation(value = "根据用户ID查询当前用户售后信息分页查询")
    public Result findBuyIdShouHou(@RequestBody PageServeShouHouForm form)throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        PageServeShouHouParam param = BeanUtil.toBean(form, PageServeShouHouParam.class);
        param.setUserId(userId);
        Page<ServeShouhou> buyIdShouHou = serveShouhouService.findBuyIdShouHou(param);
        return  Result.getResult().setMsg("查询成功").setData(buyIdShouHou);
    }

    @PostMapping("/finByIdShouHou")
    @ApiOperation(value = "根据ID查询售后信息详情")
    public Result finByIdShouHou(Integer shouHouId)throws Exception{
        ServeShouhou shouhou = serveShouhouService.getById(shouHouId);
        if (shouhou==null)throw new ServeException(3000,"没有当前ID售后信息");
        return Result.getResult().setMsg("查询成功").setData(shouhou);
    }

    @PostMapping("/finishShouhou.md")
    @ApiOperation(value = "完成售后")
    public Result finishShouhou(Integer shouHouId)throws Exception{
        serveShouhouService.finishShouhou(shouHouId);
        return Result.getResult().setMsg("完成");
    }
    @PostMapping("/CancelShouhou")
    @ApiOperation(value = "取消售后")
    public Result CancelShouhou(Integer shouHouId)throws Exception{
        serveShouhouService.CancelShouhou(shouHouId);
        return Result.getResult().setMsg("完成");
    }


    @PostMapping("/getOrderInfoPage.md")
    @ApiOperation(value = "查询当前门店所有订单售后")
    public Result getOrderInfoPage(@RequestBody PageStoresShouHouForm form)throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        PageStoresShouHouParam param = BeanUtil.toBean(form, PageStoresShouHouParam.class);
        param.setClerkId(clerkId);
        Page<ServeShouhou> orderInfoPage = serveShouhouService.getOrderInfoPage(param);
        return  Result.getResult().setMsg("查询成功").setData(orderInfoPage);
    }

}

